(function(){

YAHOO.pendular.MenuBar = function(config){
    var me = this;
    if (!config) {
        config = {};
    }
    YAHOO.lang.augmentObject(config, {
        target: "pendular-toolbar",
        autosubmenudisplay: true,
        lazyload: false,
    });
    me.config = config;
    YAHOO.pendular.MenuBar.superclass.constructor.call(me, me.config.target, config);
    me.folderMenu = me.addItem({
        id:"pendular-folder-menu",
        text: "<span class=\"pendular-result-type-directory\">&#160;&#160;&#160;&#160;</span>",
        onclick: {
            fn: function(){
                me.onFolderMenuItemClicked.fire();
            }
        }
    });
    me.searchMenu = me.addItem({
        id:"pendular-search-menu",
        text: "<span class=\"pendular-find-icon\">&#160;&#160;&#160;&#160;</span>",
        onclick: {
            fn: function(){
                me.onSearchMenuItemClicked.fire();
            }
        }
    });
    me.closeMenu = me.addItem({
        id:"pendular-close-menu",
        text: "<span class=\"pendular-clear-icon\">&#160;&#160;&#160;</span>",
        onclick: {
            fn: function(){
                me.onCloseMenuItemClicked.fire();
            }
        }
    });
    me.onFolderMenuItemClicked = new YAHOO.util.CustomEvent("folderMenuItemClicked");
    me.onWindowMenuItemClicked = new YAHOO.util.CustomEvent("windowMenuItemClicked");
    me.onSearchMenuItemClicked = new YAHOO.util.CustomEvent("searchMenuItemClicked");
    me.onCloseMenuItemClicked = new YAHOO.util.CustomEvent("closeMenuItemClicked");
    me.render();
};

YAHOO.lang.extend(YAHOO.pendular.MenuBar, YAHOO.widget.MenuBar, {
    initWindowMenu: function(){
        var me = this;
        me.windowMenu = me.addItem({
            id: "pendular-window-menu",
            text: "Window",
            submenu: {
                id: "pendular-window-submenu",
                itemdata: []
            }
        });
        me.render();
    },
    getWindowMenu: function(create){
        var wm = this.windowMenu;
        if (!wm) {
            if (create) {
                this.initWindowMenu();
            }
            else {
                return null;
            }
        }
        return this.windowMenu.cfg.getProperty("submenu");
    },
    getWindowMenuItemById: function(id){
        var i = null;
        this.eachWindowMenuItem(function(item){
            if (item.id===id) {
                i = item;
                return false;
            }
        });
        return i;
    },
    eachWindowMenuItem: function(callback){
        var wm = this.getWindowMenu();
        if (wm) {
            var items = wm.getItems(),
                i, numItems = items.length, item;
            for (i=0; i<numItems; i++){
                if (callback(items[i])===false) {
                    return;
                }
            }
        }   
    },
    removeWindowMenuItem: function(key){
        var me = this, wmi,
            wm = me.getWindowMenu()
        ;
        if (wm) {
            wmi = this.getWindowMenuItemById(key);
            if (wmi) {
                wm.removeItem(wmi);
            }
        }
    },
    checkWindowMenuItem: function(key) {
        this.eachWindowMenuItem(function(item){
            item.cfg.setProperty("checked", item.id===key);
        });
    },
    addWindowMenuItemForLink: function(link) {
        var me = this,
            wm = me.getWindowMenu(true),
            key = link.title
        ;
        var item = wm.addItem({
            text: link.innerHTML,
            id: "pendular-menuitem-" + key,
            url: link.href,
            classname: link.parentNode.className,
            checked: true,
            onclick: {
                fn: function(){
                    me.onWindowMenuItemClicked.fire(key, item, link);
                }
            }
        })._oAnchor.setAttribute("onclick", "return false;");
        me.render();
    }
});

})();
